﻿// Realty Module
var REALTY_TAB_ID = "realty_tab";

// Create an empty object for Saint-Petersburg Realty
var spbRealty = null;

//Object Realty
function Realty(name){
	this.name = name;
	this.primary = new RealtyData();
	this.secondary = new RealtyData();
}
//RealtyData Object;
function RealtyData(){
	this.date;
	this.price;
	this.change;
}

// this function starts the process of receiving data
function displayRealtyInfo()
{
	showLoadingInfo(REALTY_TAB_ID);
	_IG_FetchContent('http://data.miel.ru/informers/get.php?id=3', parseMoscowAndMosRegionHtml);
}

// this function creates objects moscowRealty & mosRegionRealty (information about realty in this cities)
function parseMoscowAndMosRegionHtml(htmlPageCode)
{
	// Display empty table
	displayRealtyData();
	// check if we can access the source data
	if (htmlPageCode == null) return;

	// Parse Moscow Realty Source Data
	var dateRegExp = /\d{2}\.\d{2}\.\d{2}(?!\d)/g;
	var date = htmlPageCode.match(dateRegExp);
	var priceRegExp = /&nbsp;\d{3,6}(?=<\/td>)/g;
	var price = htmlPageCode.match(priceRegExp);
	var changeRegExp = /-?\d+\,\d(?=<\/td>)/g;
	var change = htmlPageCode.match(changeRegExp);
	// Create an empty object for Moscow Realty
	var moscowRealty = new Realty(prefs.getMsg("moscow"));
	// Fill empty object with parsed values
	moscowRealty.primary.date = date[3].substr(0, 5);
	moscowRealty.primary.price = price[2].substr(6);
	moscowRealty.primary.change = change[2] + "0";
	moscowRealty.secondary.date = date[1].substr(0, 5);
	moscowRealty.secondary.price = price[0].substr(6);
	moscowRealty.secondary.change = change[0] + "0";
	// Display Moscow Realty Data
	appendDisplayData(moscowRealty, "moscow");
	// Create an empty object for Moscow Region Realty
	var mosRegionRealty = new Realty(prefs.getMsg("mosreg"));
	// Fill empty object with parsed values
	mosRegionRealty.primary.date = date[3].substr(0, 5);
	mosRegionRealty.primary.price = price[3].substr(6);
	mosRegionRealty.primary.change = change[3] + "0";
	mosRegionRealty.secondary.date = date[1].substr(0, 5);
	mosRegionRealty.secondary.price = price[1].substr(6);
	mosRegionRealty.secondary.change = change[1] + "0";
	// Display Moscow Region Realty Data
	appendDisplayData(mosRegionRealty, "mosreg");
	_IG_FetchContent('http://www.bn.ru/graphs/bn_graph.phtml?singlgraph=Iall', parseSPbPrimHtml);
}

function parseSpbHtml(htmlPageCode)
{
	// Parse Saint-Petersburg Realty Source Data
	var dateRegExp = /\d{2}\.\d{2}\.\d{4}/g;
	var date = htmlPageCode.match(dateRegExp);
	var priceRegExp = /\d{5}(?=(\.\d{1,2})?<\/TD>)/g;
	var price = htmlPageCode.match(priceRegExp);
	var changeRegExp = /-?\d\.\d{1,2}(?=%<\/TD>)/g;
	var change = htmlPageCode.match(changeRegExp);
	
	var realtyData = new RealtyData();
	realtyData.date = date[date.length - 1].substr(0, 5);
	realtyData.price = price[price.length - 1];
	realtyData.change = change[change.length - 1];
	return realtyData;
}

// this function creates first part of the object spbRealty (information about realty in SPb)
function parseSPbPrimHtml(htmlPageCode)  // primary realty
{
	// check if we can access the source data
	if (htmlPageCode == null) return;
	// Parse Primary Saint-Petersburg Realty Source Data
	spbRealty =	new Realty(prefs.getMsg("spb"));
	spbRealty.primary = parseSpbHtml(htmlPageCode);
	// Retrieving Saint-Petersburg Secondary Realty Data
	_IG_FetchContent('http://www.bn.ru/graphs/bn_graph.phtml?singlgraph=IIall', parseSPbSecHtml);
}

// this function creates second part of the object spbRealty (information about realty in SPb)
function parseSPbSecHtml(htmlPageCode) // secondary realty
{
	// check if we can access the source data
	if (htmlPageCode == null) return;
	// Append Secondary Realty Data to spbRealty object
	spbRealty.secondary = parseSpbHtml(htmlPageCode);
	// Display Saint-Petersburg Realty Data
	appendDisplayData(spbRealty, "spb");
}

// creating the header of output table
function getRealtyTableHead()
{
	// Output table header
	var thead = "<thead><tr>";
	thead += "<td>" + prefs.getMsg("city_region") + "</td>";
	thead += "<td>" + prefs.getMsg("date") + "</td>";
	thead += "<td>" + prefs.getMsg("price") + "</td>";
	thead += "<td>" + prefs.getMsg("perc_change") + "</td>";
	thead += "</tr></thead>";
	return thead;
}

// creating the body of output table
function getRealtyTableBody()
{
	var tbody = "<tbody>";
	// primary realty header
	tbody += "<tr><td colspan='4' align='center'><span class='header'>"+prefs.getMsg("primary")+"</span></td></tr>";
	// empty primary realty body
	tbody += "<tr class='even'><td id='moscowPrimName' nowrap><b>" + prefs.getMsg("moscow") + "</b></td>";
	tbody += "<td id='moscowPrimDate' align='center'></td>";
	tbody += "<td id='moscowPrimPrice' align='right'></td>";
	tbody += "<td id='moscowPrimDifference' align='right'></td></tr>";
	tbody += "<tr class='odd'><td id='mosregPrimName' nowrap><b>" + prefs.getMsg("mosreg") + "</b></td>";
	tbody += "<td id='mosregPrimDate' align='center'></td>";
	tbody += "<td id='mosregPrimPrice' align='right'></td>";
	tbody += "<td id='mosregPrimDifference' align='right'></td></tr>";
	tbody += "<tr class='even'><td id='spbPrimName' nowrap><b>" + prefs.getMsg("spb") + "</b></td>";
	tbody += "<td id='spbPrimDate' align='center'></td>";
	tbody += "<td id='spbPrimPrice' align='right'></td>";
	tbody += "<td id='spbPrimDifference' align='right'></td></tr>";
	// secondary realty header
	tbody += "<tr><td colspan='4' align='center'><span class='header'>"+prefs.getMsg("secondary")+"</span></td></tr>";
	// empty secondary realty body
	tbody += "<tr class='even'><td id='moscowSecName' nowrap><b>" + prefs.getMsg("moscow") + "</b></td>";
	tbody += "<td id='moscowSecDate' align='center'></td>";
	tbody += "<td id='moscowSecPrice' align='right'></td>";
	tbody += "<td id='moscowSecDifference' align='right'></td></tr>";
	tbody += "<tr class='odd'><td id='mosregSecName' nowrap><b>" + prefs.getMsg("mosreg") + "</b></td>";
	tbody += "<td id='mosregSecDate' align='center'></td>";
	tbody += "<td id='mosregSecPrice' align='right'></td>";
	tbody += "<td id='mosregSecDifference' align='right'></td></tr>";
	tbody += "<tr class='even'><td id='spbSecName' nowrap><b>" + prefs.getMsg("spb") + "</b></td>";
	tbody += "<td id='spbSecDate' align='center'></td>";
	tbody += "<td id='spbSecPrice' align='right'></td>";
	tbody += "<td id='spbSecDifference' align='right'></td></tr>";
	tbody += "</tbody>";
	return tbody;
}

// this function adds the table source html code to the Gadget code
function displayRealtyData()
{
	var copyright = "<div class='copyright'>" + 
										prefs.getMsg("copyright") + " <a href='http://www.miel.ru' target='_blank'>http://www.miel.ru</a><br>" +
										"<a href='http://www.bn.ru' target='_blank'>http://www.bn.ru</a>" +
									"</div>";	

	var realtyTable = "<table class='rate_table'>" + getRealtyTableHead() + getRealtyTableBody() + "</table>";
	// Display empty output table
	_gel(REALTY_TAB_ID).innerHTML = realtyTable + copyright;
	// Tells gadget to resize itself
	_IG_AdjustIFrameHeight();
}

//this function appends data to the output table
function appendDisplayData(data, city)
{
	// display primary realty
	_gel(city + 'PrimDate').innerHTML = data.primary.date;
	_gel(city + 'PrimPrice').innerHTML = "<b>" + formatValueToRegStd(data.primary.price) + "</b>";
	_gel(city + 'PrimDifference').innerHTML = "<span class='" + ((data.primary.change.charAt(0) == "-") ?  "decrease'>" : "increase'>+") +
												formatValueToRegStd(data.primary.change) + "</span>";
	//display secondary realty
	_gel(city + 'SecDate').innerHTML = data.secondary.date;
	_gel(city + 'SecPrice').innerHTML = "<b>" + formatValueToRegStd(data.secondary.price) + "</b>";
	_gel(city + 'SecDifference').innerHTML = "<span class='" + ((data.secondary.change.charAt(0) == "-") ?  "decrease'>" : "increase'>+") +
												formatValueToRegStd(data.secondary.change) + "</span>";
}